﻿using Avalonia.Controls;
using Avalonia.Threading;
using Bilibili.Common.Log;
using System;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.Libs.Window
{
    public abstract class ChildViewBase : UserControl
    {
        public ChildViewBase() : base() { }
        protected override void OnInitialized()
        {
            base.OnInitialized();
            AfterInitializeComponent();
        }
        public abstract void Callback(object data);
        public abstract void AfterInitializeComponent();
        protected void RunOnUiThread(Action action)
        {
            Dispatcher.UIThread.InvokeAsync(() =>
            {
                try
                {
                    action?.Invoke();
                }
                catch (Exception ex)
                {
                    LogManager.Instance.LogError("RunOnUiThread", ex);
                }
            });
        }
        protected async void RunOnUiThread(Func<Task> action)
        {
            await Dispatcher.UIThread.InvokeAsync(() =>
            {
                try
                {
                    action?.Invoke();
                }
                catch (Exception ex)
                {
                    LogManager.Instance.LogError("RunOnUiThread", ex);
                }
            });
        }
    }
}